home *** CD-ROM | disk | FTP | other *** search
Text File | 2000-10-06 | 46.9 KB | 1,782 lines | [TEXT/CWIE] |
- ///--------------------------------------------------------------------------------------
- // SpriteTest.c
- //
- // By: Tony Myles
- //
- // Extensive modifications by Karl Bunker
- //
- ///--------------------------------------------------------------------------------------
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #include <Fonts.h>
- #include <TextUtils.h>
- #include <Timer.h>
- #include <Gestalt.h>
- #include <ColorPicker.h>
-
- #include <SWIncludes.h> // Automatically include all SpriteWorld.h files
-
- #include <SWGameUtils.h>
- #include <SWDitherDown.h>
- #include <SWDialogUtils.h>
- #include <SWApplication.h>
-
- #include "SpriteTest.h"
- #include "Application.h"
-
- #if __MWERKS__
- #include <profiler.h>
- #endif
-
- #define kBlitterTest false
-
- #define kTicksPerSecond 60.15
-
-
- #define kWorldRectInset 0 // modify to test SpriteWorld rect less than window size
-
- Boolean gGlobesVisible;
- Boolean gTitleVisible;
- Boolean gCollisionDetection;
- Boolean gInterlaced;
- Boolean gSkipOddLines;
- Boolean gClearBackground;
- Boolean gColorSprites;
-
- ProcType gWhichSpriteProc;
- ProcType gWhichOffscreenProc;
- ProcType gWhichScreenProc;
- CollType gWhichCollisionProc;
- RGBColor gSpriteColor = { 0x0000, 0x0000, 0xFFFF }; // Default color = blue
- short gMoveDelta;
- SpritePtr gMasterGlobeSprite = NULL;
-
- long gLastTestFrames;
- float gLastTestSeconds;
-
- /******************** SetupSWStuff ********************/
- OSErr SetupSWStuff(
- SpriteTestPtr* spriteTestP,
- CWindowPtr srcWindowP)
- {
- OSErr err = noErr;
- SpriteTestPtr tempSpriteTestP;
- SpriteWorldPtr spriteWorldP = NULL;
- SpriteLayerPtr globeSpriteLayerP;
- SpritePtr globeSpriteArray[kNumberOfGlobeSprites];
- SpritePtr titleSpriteP;
- SpritePtr twoSpriteP;
- PixPatHandle pixPatH = NULL;
- short spriteNum;
- short oldResRefNum, curResRefNum;
- Rect windowRect, worldRect;
-
-
- *spriteTestP = NULL;
-
- SetPort((GrafPtr)srcWindowP);
-
- windowRect = srcWindowP->portRect;
- worldRect = windowRect;
- InsetRect( &worldRect, kWorldRectInset, kWorldRectInset );
-
- ShowWindow( (WindowPtr)srcWindowP );
- TextFont( systemFont );
- TextSize( 12 );
- MoveTo( 40, 80 );
- DrawString( "\pLoading Sprites... please wait" );
-
- tempSpriteTestP = (SpriteTestPtr)NewPtrClear((Size)sizeof(SpriteTestRec));
- SWSetStickyIfError( MemError() );
-
- {
- // create the sprite world
- (void)SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, &worldRect, NULL, 0);
- }
-
- if (SWStickyError() == noErr)
- {
- tempSpriteTestP->spriteWorldP = spriteWorldP;
-
- // create the sprite layer
- (void)SWCreateSpriteLayer(&globeSpriteLayerP);
- tempSpriteTestP->globeSpriteLayerP = globeSpriteLayerP;
- }
-
- if (SWStickyError() == noErr)
- {
- oldResRefNum = CurResFile();
- curResRefNum = OpenResFile("\pSpriteTest Frames");
- SWSetStickyIfError( ResError() );
- }
-
- if (SWStickyError() == noErr)
- {
- UseResFile(curResRefNum);
-
- {
- (void)SWCreateSpriteFromSinglePict(
- spriteWorldP,
- &gMasterGlobeSprite,
- NULL,
- kBaseResID,
- kBaseResID+1,
- kGlobeHeight,
- kGlobeBorderWidth,
- kFatMask);
-
- if (SWStickyError() == noErr && spriteWorldP->pixelDepth >= 8)
- (void)SWCreateRLEData( gMasterGlobeSprite );
-
- }
-
- if (SWStickyError() == noErr)
- {
- (void)SWCreateSpriteFromSinglePict(
- spriteWorldP,
- &twoSpriteP,
- NULL,
- kBaseResID+2,
- kBaseResID+2,
- kTwoHeight,
- kTwoBorderWidth,
- kFatMask);
-
- if (SWStickyError() == noErr && spriteWorldP->pixelDepth >= 8)
- (void)SWCreateRLEData( twoSpriteP );
- }
-
- if (SWStickyError() == noErr)
- {
- if ( spriteWorldP->pixelDepth < 8 )
- {
- DitherDownPict( kBaseResID+2, twoSpriteP->sharedPictGWorld );
- LowerMaskDepth( kBaseResID+2, twoSpriteP->sharedMaskGWorld );
- }
- SWSetSpriteFrameTime( twoSpriteP, kGlobeSpriteFrameTime*4 );
- SWSetSpriteFrameRange( twoSpriteP, 0, kNumberOfTwoFrames - 1 );
- SWSetSpriteFrameAdvanceMode( twoSpriteP, kSWPatrollingMode );
- }
-
- UseResFile(oldResRefNum);
- CloseResFile(curResRefNum);
- }
-
- if (SWStickyError() == noErr)
- {
- if ( SW_68K && spriteWorldP->pixelDepth >= 8 )
- {
- (void)SWCompileSprite( gMasterGlobeSprite );
- }
- }
-
- if (SWStickyError() == noErr)
- {
- // master globe sprite isn't added to any layer, so we have to lock it separately
- SWLockSprite( gMasterGlobeSprite);
- }
-
- if (SWStickyError() == noErr)
- {
-
- for (spriteNum = 0; spriteNum < kNumberOfGlobeSprites; spriteNum++)
- {
- (void)SWCloneSprite(gMasterGlobeSprite, globeSpriteArray + spriteNum, NULL);
- tempSpriteTestP->globeSpriteArray[spriteNum] = globeSpriteArray[spriteNum];
- }
-
- }
-
- if (SWStickyError() == noErr)
- {
- (void)SWCreateSpriteFromPictResource(
- spriteWorldP,
- &titleSpriteP,
- NULL,
- kBaseResID,
- kBaseResID,
- 1,
- kFatMask);
-
- if ( spriteWorldP->pixelDepth < 8 )
- {
- DitherDownPict( kBaseResID, titleSpriteP->frameArray[0]->framePort );
- LowerMaskDepth( kBaseResID, titleSpriteP->frameArray[0]->maskPort );
- }
- else
- {
- if (SWStickyError() == noErr)
- (void)SWCreateRLEData( titleSpriteP );
- }
-
- tempSpriteTestP->titleSpriteP = titleSpriteP;
- tempSpriteTestP->twoSpriteP = twoSpriteP;
-
- *spriteTestP = tempSpriteTestP;
-
- for (spriteNum = 0; spriteNum < kNumberOfGlobeSprites; spriteNum++)
- {
- if (spriteNum == (kNumberOfGlobeSprites / 2))
- {
- SWAddSprite(globeSpriteLayerP, titleSpriteP);
- SWAddSprite(globeSpriteLayerP, twoSpriteP);
- }
- SWAddSprite(globeSpriteLayerP, globeSpriteArray[spriteNum]);
- }
-
-
- SWAddSpriteLayer(spriteWorldP, globeSpriteLayerP);
-
-
- SWLockSpriteWorld(spriteWorldP);
-
- SWSetPortToBackground(spriteWorldP);
- EraseRect( &spriteWorldP->backRect );
-
- if ( spriteWorldP->pixelDepth >= 8 )
- {
- pixPatH = GetPixPat(kBackDropPixPatID);
- if ( pixPatH != NULL )
- {
- FillCRect(&spriteWorldP->backRect, pixPatH);
- DisposePixPat(pixPatH);
- }
- }
- else
- {
- FillRect(&spriteWorldP->backRect, &qd.ltGray);
- }
-
- SWSetPortToWindow(spriteWorldP);
-
- SetupSpriteWorldElements(tempSpriteTestP);
- }
- if ( SWStickyError() != noErr)
- {
- DisposeSWStuff(tempSpriteTestP);
- }
-
- return SWStickyError();
- }
-
-
- /******************** DisposeSWStuff ********************/
- void DisposeSWStuff(
- SpriteTestPtr spriteTestP)
- {
- if (spriteTestP != NULL)
- {
- if (spriteTestP->spriteWorldP != NULL)
- {
- SWDisposeSpriteWorld(&spriteTestP->spriteWorldP);
- }
- // gMasterGlobeSprite is not added to any layer,
- // so SWDisposeSpriteWorld won't dispose of it
- if ( gMasterGlobeSprite != NULL )
- {
- SWDisposeSprite( &gMasterGlobeSprite );
- }
- DisposePtr((Ptr)spriteTestP);
- }
- }
-
-
- /******************** SetupSpriteWorldElements ********************/
- void SetupSpriteWorldElements(
- SpriteTestPtr spriteTestP)
- {
- register long spriteNum;
- Rect moveBoundsRect;
- Rect titleDestRect;
-
-
- moveBoundsRect = spriteTestP->spriteWorldP->backRect;
- InsetRect(&moveBoundsRect,-kWorldRectInset,-kWorldRectInset);
-
- // set up the globe sprites
- gMoveDelta = 0;
- for (spriteNum = 0; spriteNum < kNumberOfGlobeSprites; spriteNum++)
- {
- SetupGlobeSprite(spriteTestP->globeSpriteArray[spriteNum], &moveBoundsRect,
- GetRandom(moveBoundsRect.left, moveBoundsRect.right),
- GetRandom(moveBoundsRect.top, moveBoundsRect.bottom));
- }
-
- // set up the title sprites
- titleDestRect = spriteTestP->titleSpriteP->destFrameRect;
-
- CenterRect( &titleDestRect, &moveBoundsRect );
- OffsetRect( &titleDestRect, -(kTwoHeight + 2 ), 0 );
- SWSetSpriteLocation(spriteTestP->titleSpriteP, titleDestRect.left, titleDestRect.top );
-
- SWSetSpriteLocation(spriteTestP->twoSpriteP, titleDestRect.right, titleDestRect.top-7 );
-
- SWSetSpriteWorldMaxFPS( spriteTestP->spriteWorldP, 30 );
- SWSetCleanUpSpriteWorld(spriteTestP->spriteWorldP);
-
- gGlobesVisible = true;
- gTitleVisible = true;
- gCollisionDetection = false;
- gWhichCollisionProc = kRLECollisionProc;
- gInterlaced = false;
- gSkipOddLines = kSkipOddLines;
- gClearBackground = false;
- gColorSprites = false;
-
- SetDefaultTestDrawProcs(spriteTestP->spriteWorldP->pixelDepth);
-
- for (spriteNum = 0; spriteNum < kNumberOfGlobeSprites; spriteNum++)
- SWSetSpriteVisible( spriteTestP->globeSpriteArray[spriteNum], gGlobesVisible );
-
- SWSetSpriteVisible( spriteTestP->titleSpriteP, gTitleVisible );
- SWSetSpriteVisible( spriteTestP->twoSpriteP, gTitleVisible );
- }
-
-
- /******************** SetDefaultTestDrawProcs ********************/
-
- void SetDefaultTestDrawProcs( short depth )
- {
- gWhichSpriteProc = kCopyBitsProc;
- gWhichOffscreenProc = kCopyBitsProc;
- gWhichScreenProc = kCopyBitsProc;
-
- // Set the default DrawProcs to BlitPixie if possible
- if (depth >= 8 || SW_68K)
- {
- gWhichOffscreenProc = kBlitPixieProc;
- gWhichScreenProc = kBlitPixieProc;
- }
-
- if (depth >= 8)
- {
- if (SW_68K)
- gWhichSpriteProc = kCompiledProc;
- else
- gWhichSpriteProc = kRLEProc;
- }
- else if (SW_68K)
- {
- gWhichSpriteProc = kBlitPixieProc;
- }
- }
-
-
- /******************** SetupGlobeSprite ********************/
-
- void SetupGlobeSprite(
- SpritePtr globeSpriteP,
- Rect *moveBoundsRect,
- short horizLocation,
- short vertLocation)
- {
- Rect tempBoundsRect;
- short horizMoveDelta;
- short vertMoveDelta;
-
- gMoveDelta++;
- if ( gMoveDelta > 17 )
- gMoveDelta = 1;
-
- tempBoundsRect = *moveBoundsRect;
-
- horizMoveDelta = GetRandom(2, 6); // gMoveDelta
- vertMoveDelta = GetRandom(2, 6); // gMoveDelta
-
- horizMoveDelta = 1;
- vertMoveDelta = 1;
-
- if (GetRandom(0, 1) == 0)
- {
- horizMoveDelta = -horizMoveDelta;
- }
-
- if (GetRandom(0, 1) == 0)
- {
- vertMoveDelta = -vertMoveDelta;
- }
-
- // set the sprite’s movement characteristics
- SWSetSpriteMoveBounds(globeSpriteP, &tempBoundsRect);
- SWSetSpriteMoveDelta(globeSpriteP, horizMoveDelta, vertMoveDelta);
- SWSetSpriteMoveProc(globeSpriteP, GlobeSpriteMoveProc);
- SWSetSpriteMoveTime(globeSpriteP, kGlobeSpriteMoveTime);
-
- SWSetSpriteFrameTime(globeSpriteP, kGlobeSpriteFrameTime);
- SWSetSpriteFrameRange(globeSpriteP, 0, kNumberOfGlobeFrames - 1);
- SWSetSpriteFrameAdvance(globeSpriteP, GetRandom(0, 1) ? -1 : 1);
-
- SWSetSpriteCollideProc(globeSpriteP, RegionBounceCollideProc);
- SWSetSpriteColor( globeSpriteP, &gSpriteColor );
-
- // set the sprite’s initial location
- SWSetSpriteLocation(globeSpriteP, horizLocation, vertLocation);
- }
-
-
- /******************** SpriteTestIdle ********************/
- void SpriteTestIdle(
- SpriteTestPtr spriteTestP )
- {
- if (gCollisionDetection)
- {
- SWCollideSpriteLayer(spriteTestP->spriteWorldP,
- spriteTestP->globeSpriteLayerP, spriteTestP->globeSpriteLayerP);
- }
- SWProcessSpriteWorld( spriteTestP->spriteWorldP );
- SWAnimateSpriteWorld( spriteTestP->spriteWorldP );
- }
-
-
- /******************** UpdateSpriteTest ********************/
- void UpdateSpriteTest(
- SpriteTestPtr spriteTestP,
- WindowPtr updateWindowP)
- {
- SetPort( updateWindowP );
- if ( updateWindowP->portRect.right - updateWindowP->portRect.left >
- spriteTestP->spriteWorldP->backRect.right -
- spriteTestP->spriteWorldP->backRect.left )
- {
- ForeColor( blueColor );
- PaintRect( &updateWindowP->portRect );
- }
- ForeColor( blackColor );
- SWUpdateSpriteWorld(spriteTestP->spriteWorldP, true);
- }
-
- #pragma mark -
-
- /******************** HandleCreateSpriteCommand ********************/
- void HandleCreateSpriteCommand(
- SpriteTestPtr spriteTestP)
- {
- OSErr err;
- SpritePtr globeSpriteP;
- Rect moveBoundsRect;
- Point mouseLocation;
-
-
- err = SWCloneSprite(gMasterGlobeSprite, &globeSpriteP, NULL);
-
- if (err == noErr)
- {
- moveBoundsRect = spriteTestP->spriteWorldP->backRect;
- GetMouse(&mouseLocation);
- mouseLocation.h -= spriteTestP->spriteWorldP->windRect.left;
- mouseLocation.v -= spriteTestP->spriteWorldP->windRect.top;
-
- SetupGlobeSprite(globeSpriteP, &moveBoundsRect, mouseLocation.h, mouseLocation.v);
-
- // Add sprite in front of or behind the title sprite
- if ( GetRandom(0,1) )
- SWAddSprite(spriteTestP->globeSpriteLayerP, globeSpriteP);
- else
- SWInsertSpriteBeforeSprite(globeSpriteP, spriteTestP->titleSpriteP);
- }
- }
-
-
- /******************** HandleSpriteTestTitleCommand ********************/
- void HandleSpriteTestTitleCommand(
- SpriteTestPtr spriteTestP)
- {
- gTitleVisible = !gTitleVisible;
-
- SWSetSpriteVisible(spriteTestP->titleSpriteP, gTitleVisible);
- SWSetSpriteVisible(spriteTestP->twoSpriteP, gTitleVisible);
- }
-
-
- /******************** HandleBouncingBallsCommand ********************/
- void HandleBouncingBallsCommand(
- SpriteTestPtr spriteTestP)
- {
- SpritePtr globeSpriteP;
-
-
- gGlobesVisible = !gGlobesVisible;
-
- globeSpriteP = NULL;
- while ((globeSpriteP = SWGetNextSprite(spriteTestP->globeSpriteLayerP, globeSpriteP)) != NULL)
- {
- if (globeSpriteP != spriteTestP->titleSpriteP && globeSpriteP != spriteTestP->twoSpriteP)
- {
- SWSetSpriteVisible(globeSpriteP, gGlobesVisible);
- }
- }
- }
-
-
- /******************** RemoveClickedSprite ********************/
- void RemoveClickedSprite(
- SpriteTestPtr spriteTestP)
- {
- Point mouseLocation;
- SpritePtr spriteToRemove;
-
-
- if ( spriteTestP != NULL )
- {
- GetMouse(&mouseLocation);
-
- mouseLocation.h -= spriteTestP->spriteWorldP->windRect.left;
- mouseLocation.v -= spriteTestP->spriteWorldP->windRect.top;
- // if a sprite has been clicked on, and it isn't the title sprite,
- // then remove it and dispose of it
- spriteToRemove = SWFindSpriteByPoint(spriteTestP->globeSpriteLayerP, NULL, mouseLocation);
- if ( spriteToRemove != NULL &&
- spriteToRemove != spriteTestP->titleSpriteP &&
- spriteToRemove != spriteTestP->twoSpriteP)
- {
- SWRemoveSpriteFromAnimation( spriteTestP->spriteWorldP, spriteToRemove, true );
- }
- }
- }
-
- #pragma mark -
-
- /******************** SetUpTestDialog ********************/
- void SetUpTestDialog(
- SpriteTestPtr spriteTestP)
- {
- GrafPtr savePort;
- DialogPtr theDialog;
- short itemHit;
- ProcType originalSpriteProc,
- originalOffscreenProc,
- originalScreenProc;
- Boolean runNow;
- Boolean done = false;
- OSErr err = noErr;
-
-
- GetPort( &savePort );
-
- theDialog = GetNewDialog(kSetUpTestResID, nil, (WindowPtr)-1L);
-
- SetDialogDefaultItem( theDialog, ok );
- SetDialogCancelItem( theDialog, cancel );
-
- originalSpriteProc = gWhichSpriteProc;
- originalOffscreenProc = gWhichOffscreenProc;
- originalScreenProc = gWhichScreenProc;
-
- // Check if we can do 8-bit-specific tests
- #if !SW_68K
- SetDItemHilite( theDialog, kBlitPixieSpriteButton,
- (spriteTestP->spriteWorldP->pixelDepth >= 8) );
- SetDItemHilite( theDialog, kBlitPixieOffscreenButton,
- (spriteTestP->spriteWorldP->pixelDepth >= 8) );
- SetDItemHilite( theDialog, kBlitPixieScreenButton,
- (spriteTestP->spriteWorldP->pixelDepth >= 8) );
- #endif
-
- SetDItemHilite( theDialog, kRLESpriteButton,
- (spriteTestP->spriteWorldP->pixelDepth >= 8) );
-
- #if SW_68K
- SetDItemHilite( theDialog, kCompiledSpriteButton,
- (spriteTestP->spriteWorldP->pixelDepth >= 8) );
- #else
- SetDItemHilite( theDialog, kCompiledSpriteButton, false );
- #endif
-
-
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kRunTestNowCheckBox), true );
-
- SetSetUpTestButtons( theDialog );
-
- ShowWindow( theDialog );
-
- while ( !done )
- {
- ModalDialog( nil, &itemHit );
- switch( itemHit )
- {
- case kCopyBitsSpriteButton:
- gWhichSpriteProc = kCopyBitsProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kBlitPixieSpriteButton:
- gWhichSpriteProc = kBlitPixieProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kRLESpriteButton:
- gWhichSpriteProc = kRLEProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kCompiledSpriteButton:
- gWhichSpriteProc = kCompiledProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kCopyBitsOffscreenButton:
- gWhichOffscreenProc = kCopyBitsProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kBlitPixieOffscreenButton:
- gWhichOffscreenProc = kBlitPixieProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kCopyBitsScreenButton:
- gWhichScreenProc = kCopyBitsProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kBlitPixieScreenButton:
- gWhichScreenProc = kBlitPixieProc;
- SetSetUpTestButtons( theDialog );
- break;
- case kRunTestNowCheckBox:
- ToggleDItemValue(theDialog, kRunTestNowCheckBox);
- break;
- case kInterlacedCheckBox:
- gInterlaced = !gInterlaced;
- SetSetUpTestButtons( theDialog );
- break;
- case kSkipOddLinesRadioButton:
- gSkipOddLines = kSkipOddLines;
- SetSetUpTestButtons( theDialog );
- break;
- case kSkipEvenLinesRadioButton:
- gSkipOddLines = kSkipEvenLines;
- SetSetUpTestButtons( theDialog );
- break;
- case kClearBackgroundCheckBox:
- gClearBackground = !gClearBackground;
- SetSetUpTestButtons( theDialog );
- break;
- case kColorSpritesCheckBox:
- gColorSprites = !gColorSprites;
- SetSetUpTestButtons( theDialog );
- break;
- case kSetClearColorButton:
- {
- Point where = {0,0};
- StringPtr prompt = "\pColor to Fill Background with:";
- RGBColor newColor = spriteTestP->spriteWorldP->backgroundColor;
-
- if ( GetColor( where, prompt, &spriteTestP->spriteWorldP->backgroundColor, &newColor ) )
- {
- SWSetBackgroundColor( spriteTestP->spriteWorldP, &newColor );
- }
-
- SWUpdateWindow(spriteTestP->spriteWorldP );
- }
- break;
- case kSetSpriteColorButton:
- {
- Point where = {0,0};
- StringPtr prompt = "\pColor to Fill Sprites with:";
- SpritePtr srcSpriteP;
-
- if ( GetColor( where, prompt, &gSpriteColor, &gSpriteColor ) )
- {
- srcSpriteP = SWGetNextSprite(spriteTestP->globeSpriteLayerP, NULL);
-
- while (srcSpriteP != NULL)
- {
- SWSetSpriteColor( srcSpriteP, &gSpriteColor );
- srcSpriteP = SWGetNextSprite(spriteTestP->globeSpriteLayerP,
- srcSpriteP);
- }
- }
-
- SWUpdateWindow(spriteTestP->spriteWorldP );
- }
- break;
- case ok:
- runNow = GetControlValue( (ControlHandle)GetDItemHandle(theDialog, kRunTestNowCheckBox));
- case cancel:
- done = true;
- break;
- }
- }
- DisposeDialog( theDialog );
- SetPort( savePort );
-
- if ( itemHit == ok )
- {
- if ( runNow )
- {
- SWSetPortToWindow( spriteTestP->spriteWorldP );
- RunTheTest( spriteTestP );
-
- DisplayPerformance(gLastTestFrames, gLastTestSeconds);
- }
- }
- if ( itemHit == cancel )
- {
- gWhichSpriteProc = originalSpriteProc;
- gWhichOffscreenProc = originalOffscreenProc;
- gWhichScreenProc = originalScreenProc;
- }
- }
-
-
-
- /******************** SetSetUpTestButtons ********************/
- void SetSetUpTestButtons(
- DialogPtr theDialog)
- {
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kCopyBitsSpriteButton),
- (gWhichSpriteProc == kCopyBitsProc) );
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kBlitPixieSpriteButton),
- (gWhichSpriteProc == kBlitPixieProc) );
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kRLESpriteButton),
- (gWhichSpriteProc == kRLEProc) );
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kCompiledSpriteButton),
- (gWhichSpriteProc == kCompiledProc) );
-
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kCopyBitsOffscreenButton),
- (gWhichOffscreenProc == kCopyBitsProc) );
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kBlitPixieOffscreenButton),
- (gWhichOffscreenProc == kBlitPixieProc) );
-
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kCopyBitsScreenButton),
- (gWhichScreenProc == kCopyBitsProc) );
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kBlitPixieScreenButton),
- (gWhichScreenProc == kBlitPixieProc) );
-
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kInterlacedCheckBox),
- gInterlaced );
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kSkipOddLinesRadioButton),
- (gSkipOddLines == kSkipOddLines) );
- SetDItemHilite( theDialog, kSkipOddLinesRadioButton, gInterlaced );
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kSkipEvenLinesRadioButton),
- (gSkipOddLines == kSkipEvenLines) );
- SetDItemHilite( theDialog, kSkipEvenLinesRadioButton, gInterlaced );
-
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kClearBackgroundCheckBox),
- gClearBackground );
- SetDItemHilite( theDialog, kSetClearColorButton, gClearBackground );
-
- SetControlValue( (ControlHandle)GetDItemHandle(theDialog, kColorSpritesCheckBox),
- gColorSprites );
- SetDItemHilite( theDialog, kSetSpriteColorButton, gColorSprites );
- }
-
- /******************** CollisionProcDialog ********************/
-
- void CollisionProcDialog(
- SpriteTestPtr spriteTestP)
- {
- Boolean originalCollisionOn = gCollisionDetection;
- CollType originalCollisionType = gWhichCollisionProc;
- GrafPtr savePort;
- DialogPtr theDialog;
- Boolean done = false;
- short itemHit;
-
- GetPort( &savePort );
-
- theDialog = GetNewDialog(kCollisionProcResID, nil, (WindowPtr)-1L);
- if ( theDialog == NULL )
- return;
-
- SetDialogDefaultItem( theDialog, ok );
- SetDialogCancelItem( theDialog, cancel );
-
- SetCollisionProcButtons( theDialog );
-
- SetDItemHilite( theDialog, kPixelCollisionButton,
- (spriteTestP->spriteWorldP->pixelDepth >= 8) );
- SetDItemHilite( theDialog, kRLECollisionButton,
- (spriteTestP->spriteWorldP->pixelDepth >= 8) );
-
- ShowWindow( theDialog );
-
- while ( !done )
- {
- ModalDialog( nil, &itemHit );
-
- switch ( itemHit )
- {
- case ok:
- case cancel:
- done = true;
- break;
- case kCollisionDetectionCheckbox:
- gCollisionDetection = !gCollisionDetection;
- SetCollisionProcButtons( theDialog );
- break;
- case kRectCollisionButton:
- gWhichCollisionProc = kRectCollisionProc;
- SetCollisionProcButtons( theDialog );
- break;
- case kRadiusCollisionButton:
- gWhichCollisionProc = kRadiusCollisionProc;
- SetCollisionProcButtons( theDialog );
- break;
- case kPixelCollisionButton:
- gWhichCollisionProc = kPixelCollisionProc;
- SetCollisionProcButtons( theDialog );
- break;
- case kRegionCollisionButton:
- gWhichCollisionProc = kRegionCollisionProc;
- SetCollisionProcButtons( theDialog );
- break;
- case kRLECollisionButton:
- gWhichCollisionProc = kRLECollisionProc;
- SetCollisionProcButtons( theDialog );
- break;
- }
- }
-
- if ( itemHit == cancel )
- {
- gCollisionDetection = originalCollisionOn;
- gWhichCollisionProc = originalCollisionType;
- }
-
- DisposeDialog( theDialog );
- SetPort( savePort );
-
- }
-
- void SetCollisionProcButtons( DialogPtr theDialog )
- {
- SetControlValue( (ControlHandle) GetDItemHandle(theDialog, kCollisionDetectionCheckbox),
- gCollisionDetection );
-
- SetControlValue( (ControlHandle) GetDItemHandle(theDialog, kRectCollisionButton),
- gWhichCollisionProc == kRectCollisionProc);
- SetControlValue( (ControlHandle) GetDItemHandle(theDialog, kRadiusCollisionButton),
- gWhichCollisionProc == kRadiusCollisionProc);
- SetControlValue( (ControlHandle) GetDItemHandle(theDialog, kPixelCollisionButton),
- gWhichCollisionProc == kPixelCollisionProc);
- SetControlValue( (ControlHandle) GetDItemHandle(theDialog, kRegionCollisionButton),
- gWhichCollisionProc == kRegionCollisionProc);
- SetControlValue( (ControlHandle) GetDItemHandle(theDialog, kRLECollisionButton),
- gWhichCollisionProc == kRLECollisionProc);
-
- }
-
- /******************** RunAllTests ********************/
-
- extern WindowPtr gWindowP; // in Application.c
-
- void RunAllTests(
- SpriteTestPtr spriteTestP)
- {
- StringPtr reportFileName = "\pSpriteTest "
- #if SW_68K
- "68K"
- #else
- "PPC"
- #endif
- " FPS Report";
-
- StringPtr reportTitle = "\p \015\015\015\015" // <-- Make room for header image (\015 is \r, even in MPW)
- "SpriteTest results:\015"
- "(should be viewed with a huge tab size in e.g. BBEdit,"
- " or imported into a spreadsheet)\015"
- "\015";
-
- StringPtr drawProcNames[4] =
- { "\pCopyBits", "\pBlitPixie", "\pRLE", "\pCompiled" };
-
- short depth;
- OSErr err;
- float fps;
- short ref;
- StringPtr str;
- Str255 numStr;
- Ptr buffer;
- long count;
- UInt32 tick,startTick;
- long result;
- Handle pict,oldres;
- FInfo info;
- GDHandle windowGDH;
- Rect globalRect;
- DialogPtr dialog = NULL;
- Handle snd;
-
- err = GetFInfo( reportFileName, 0, &info );
- if ( err == fnfErr )
- {
- err = Create( reportFileName, 0, 'ttxt', 'TEXT' );
- }
-
- // add header picture
- if ( err == noErr )
- {
- CreateResFile( reportFileName );
- err = ResError();
-
- if ( err == noErr || err == dupFNErr )
- {
- ref = OpenResFile( reportFileName );
- if ( ref != -1 )
- {
- UseResFile(ref);
-
- pict = GetResource( 'PICT', 130 );
- if ( pict != NULL )
- {
- DetachResource( pict );
- if ( ResError() == noErr )
- {
- oldres = Get1Resource( 'PICT', 1000 );
- if ( oldres != NULL )
- RemoveResource( oldres);
-
- AddResource( pict, 'PICT', 1000, "\p" );
- }
- CloseResFile( ref );
- }
- }
- err = noErr;
- }
- }
-
- #define WRITE_STRING(string) \
- str = string; \
- count = str[0]; \
- buffer = (Ptr) (str + 1); \
- err = FSWrite( ref, &count, buffer )
-
- if ( err == noErr )
- err = FSOpen( reportFileName, 0, &ref );
-
- if ( err == noErr )
- {
- SetEOF( ref, 0 );
-
- WRITE_STRING(reportTitle);
-
- WRITE_STRING("\pTest ran on:\t");
- GetDateTime(&startTick);
- IUDateString(startTick,longDate,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p ");
- IUTimeString(startTick,longDate,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\015");
-
- #if SW_68K
- WRITE_STRING("\pRunning:\t680X0 code\015");
- #else
- WRITE_STRING("\pRunning:\tPowerPC code\015");
- #endif
-
- Gestalt( gestaltSystemVersion, &result );
- WRITE_STRING("\pSystem Version ID:\t");
- NumToString(result,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\015");
-
- result = HiWord( SWGetSpriteWorldVersion() );
- WRITE_STRING("\pSpriteWorld Version ID:\t");
- NumToString(result,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\015");
-
- Gestalt( gestaltMachineType, &result );
- WRITE_STRING("\pMachine Gestalt ID:\t");
- NumToString(result,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\015");
-
- Gestalt( gestaltNativeCPUtype, &result );
- WRITE_STRING("\pNative CPU ID:\t");
- NumToString(result,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\015");
-
- Gestalt( gestaltPhysicalRAMSize, &result );
- WRITE_STRING("\pPhysical RAM Size:\t");
- NumToString((result / 1024) / 1024,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p MB\015");
-
- Gestalt( gestaltVMAttr, &result );
- if ( result & ( 1 << gestaltVMPresent ) )
- {
- WRITE_STRING("\pVirtual Memory:\tOn\015");
-
- Gestalt( gestaltLogicalRAMSize, &result );
-
- WRITE_STRING("\pVirtual RAM Size:\t");
- NumToString(result / (1024L * 1024L),numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p MB\015");
- }
- else
- {
- WRITE_STRING("\pVirtual Memory:\tOff\015");
- }
-
- globalRect = gWindowP->portRect;
- SetPort(gWindowP);
- LocalToGlobal( &topLeft(globalRect) );
- LocalToGlobal( &botRight(globalRect) );
- windowGDH = GetMaxDevice( &globalRect );
-
- WRITE_STRING("\pCurrent Screen Resolution:\t");
- if (windowGDH)
- {
- NumToString( (**windowGDH).gdRect.right - (**windowGDH).gdRect.left,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\px");
- NumToString( (**windowGDH).gdRect.bottom - (**windowGDH).gdRect.top,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p pixels\015");
- }
- else
- {
- WRITE_STRING("\p unknown ?\015");
- }
-
- WRITE_STRING("\p\015");
-
- // flush buffer to disk, just in case something ugly happens...
- {
- IOParam param;
- long pos;
-
- GetFPos(ref,&pos);
- SetEOF(ref,pos);
-
- param.ioCompletion = NULL;
- param.ioNamePtr = NULL;
- param.ioRefNum = ref;
-
- err = PBFlushFileSync( (ParmBlkPtr) ¶m );
- FlushVol(NULL,0);
- }
-
- WRITE_STRING("\pDepth\tScreenProc\tOffscreenProc\tSpriteProc\tFPS\015");
-
- for ( depth = 1; depth <= 32; depth *= 2 )
- {
- SWSetPortToWindow( spriteTestP->spriteWorldP );
- err = SetNewMonitorDepth( depth );
-
- if ( err == noErr )
- {
- SWSetPortToWindow( spriteTestP->spriteWorldP );
-
- for ( gWhichScreenProc = kCopyBitsProc; gWhichScreenProc <= kBlitPixieProc; gWhichScreenProc++ )
- {
- // no AllBit blitters on PPC
- if ( SW_PPC && (depth) < 8 && (gWhichScreenProc == kBlitPixieProc) )
- continue;
-
- for ( gWhichOffscreenProc = kCopyBitsProc; gWhichOffscreenProc <= kBlitPixieProc; gWhichOffscreenProc++ )
- {
- // no AllBit blitters on PPC
- if ( SW_PPC && (depth < 8) && (gWhichOffscreenProc == kBlitPixieProc) )
- continue;
-
- for ( gWhichSpriteProc = kCopyBitsProc; gWhichSpriteProc <= kCompiledProc; gWhichSpriteProc++ )
- {
- // no AllBit blitters on PPC
- if ( SW_PPC && (depth < 8) && (gWhichScreenProc == kBlitPixieProc) )
- continue;
- // no RLE or compiled blitters below 256 colors
- if ( depth < 8 && (gWhichSpriteProc == kRLEProc || gWhichSpriteProc == kCompiledProc) )
- continue;
- // compiled sprites are 68k-only
- if ( SW_PPC && (gWhichSpriteProc == kCompiledProc))
- continue;
-
- NumToString(depth,numStr);
- ParamText(
- numStr,
- drawProcNames[gWhichScreenProc],
- drawProcNames[gWhichOffscreenProc],
- drawProcNames[gWhichSpriteProc]);
-
- dialog = GetNewDialog( 201, NULL, (WindowPtr) -1 );
- if ( dialog != NULL)
- {
- DrawDialog(dialog);
- tick = TickCount();
-
- // wait 3 seconds, so user can read
- while ( TickCount() < tick + 3 * 60 )
- {
- if ( Button() )
- goto abort; // yes, goto. Flags are silly. :) // afb
- }
-
- DisposeDialog(dialog);
- dialog = NULL;
- }
- SWSetPortToWindow( spriteTestP->spriteWorldP );
- RunTheTest( spriteTestP );
-
- if ( Button() )
- goto abort;
-
- fps = (gLastTestSeconds > 0.0) ? (float) gLastTestFrames / gLastTestSeconds : gLastTestFrames;
-
- NumToString(depth,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\t");
-
- WRITE_STRING(drawProcNames[gWhichScreenProc]);
- WRITE_STRING("\p\t");
-
- WRITE_STRING(drawProcNames[gWhichOffscreenProc]);
- WRITE_STRING("\p\t");
-
- WRITE_STRING(drawProcNames[gWhichSpriteProc]);
- WRITE_STRING("\p\t");
-
- NumToString(fps,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\015");
-
- }
- }
- }
- }
- }
-
- WRITE_STRING("\p\015Test completed on:\t");
- GetDateTime(&tick);
- IUDateString(tick,longDate,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p ");
- IUTimeString(tick,longDate,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p\015");
-
- WRITE_STRING("\pRunning time:\t");
- NumToString( (tick - startTick) / 60,numStr);
- WRITE_STRING(numStr);
- WRITE_STRING("\p minutes\015");
-
- // Done! <yoohoo>
-
- snd = GetResource('snd ',128);
- if ( snd != NULL )
- SndPlay( NULL, (SndListHandle) snd, false );
- else
- SysBeep(1);
-
- abort:
-
- if ( dialog != NULL )
- {
- DisposeDialog( dialog );
- dialog = NULL;
- }
-
- err = FSClose( ref );
-
- }
-
- }
-
-
- /******************** RunTheTest ********************/
- void RunTheTest(
- SpriteTestPtr spriteTestP)
- {
- WindowPtr testWindowP = FrontWindow();
- SpritePtr curSpriteP;
- unsigned long frames, seconds;
- long ticks;
- Rect shieldRect;
- Point shieldRectOffset;
- Rect srcRect,
- destRect;
- UnsignedWide startMicroseconds,
- endMicroseconds;
-
- // set up for the test
- shieldRect = spriteTestP->spriteWorldP->windowFrameP->frameRect;
- SWSetPortToWindow(spriteTestP->spriteWorldP);
- LocalToGlobal( &topLeft(shieldRect) );
- LocalToGlobal( &botRight(shieldRect) );
- shieldRectOffset.h = shieldRectOffset.v = 0;
- ShieldCursor( &shieldRect, shieldRectOffset );
-
- SWHideMenuBar(testWindowP);
-
- UpdateSpriteTest( spriteTestP, testWindowP );
-
- curSpriteP = NULL;
- while ((curSpriteP = SWGetNextSprite(spriteTestP->globeSpriteLayerP, curSpriteP)) != NULL)
- {
- if (curSpriteP != spriteTestP->titleSpriteP && curSpriteP != spriteTestP->twoSpriteP)
- {
- SWSetSpriteFrameTime(curSpriteP, 0);
- SWSetSpriteMoveTime(curSpriteP, 0);
-
- if ( gColorSprites )
- {
- if ( spriteTestP->spriteWorldP->pixelDepth < 8 )
- {
- (void)SWSetSpriteDrawProc(curSpriteP, SWStdSpriteColorDrawProc);
- }
- else
- {
- switch ( gWhichSpriteProc )
- {
- case kCopyBitsProc:
- (void)SWSetSpriteDrawProc(curSpriteP, SWStdSpriteColorDrawProc);
- break;
- case kBlitPixieProc:
- case kCompiledProc:
- (void)SWSetSpriteDrawProc(curSpriteP, BlitPixieMaskColorDrawProc);
- break;
- case kRLEProc:
- (void)SWSetSpriteDrawProc(curSpriteP, BlitPixieRLEColorDrawProc);
- break;
- }
- }
- }
- else
- {
- if ( spriteTestP->spriteWorldP->pixelDepth < 8 )
- {
- if ( SW_68K && (gWhichSpriteProc == kBlitPixieProc || gWhichSpriteProc == kCompiledProc) )
- (void)SWSetSpriteDrawProc(curSpriteP, BlitPixieAllBitMaskDrawProc);
- else
- (void)SWSetSpriteDrawProc(curSpriteP, SWStdSpriteDrawProc);
- }
- else
- {
- switch ( gWhichSpriteProc )
- {
- case kBlitPixieProc:
- (void)SWSetSpriteDrawProc(curSpriteP, BlitPixieMaskDrawProc);
- break;
- case kRLEProc:
- (void)SWSetSpriteDrawProc(curSpriteP, BlitPixieRLEDrawProc);
- break;
- case kCompiledProc:
- (void)SWSetSpriteDrawProc(curSpriteP, BlitPixieCompiledSpriteDrawProc);
- break;
- }
- }
- }
-
- if ( gCollisionDetection )
- {
- switch ( gWhichCollisionProc )
- {
- case kRectCollisionProc:
- (void)SWSetSpriteCollideProc(curSpriteP, RectBounceCollideProc);
- break;
- case kRadiusCollisionProc:
- (void)SWSetSpriteCollideProc(curSpriteP, RadiusBounceCollideProc);
- break;
- case kPixelCollisionProc:
- if ( spriteTestP->spriteWorldP->pixelDepth >= 8 )
- (void)SWSetSpriteCollideProc(curSpriteP, PixelBounceCollideProc);
- else
- (void)SWSetSpriteCollideProc(curSpriteP, RegionBounceCollideProc);
- break;
- case kRegionCollisionProc:
- (void)SWSetSpriteCollideProc(curSpriteP, RegionBounceCollideProc);
- break;
- case kRLECollisionProc:
- if ( spriteTestP->spriteWorldP->pixelDepth >= 8 )
- (void)SWSetSpriteCollideProc(curSpriteP, RLEBounceCollideProc);
- else
- (void)SWSetSpriteCollideProc(curSpriteP, RegionBounceCollideProc);
- break;
- }
- }
- }
- }
-
- if ( gWhichSpriteProc == kBlitPixieProc || gWhichSpriteProc == kCompiledProc || gWhichSpriteProc == kRLEProc )
- {
- if ( spriteTestP->spriteWorldP->pixelDepth >= 8 )
- {
- (void)SWSetSpriteDrawProc(spriteTestP->titleSpriteP, BlitPixieMaskDrawProc);
- (void)SWSetSpriteDrawProc(spriteTestP->twoSpriteP, BlitPixieMaskDrawProc);
- }
- else if (SW_68K)
- {
- (void)SWSetSpriteDrawProc(spriteTestP->titleSpriteP, BlitPixieAllBitMaskDrawProc);
- (void)SWSetSpriteDrawProc(spriteTestP->twoSpriteP, BlitPixieAllBitMaskDrawProc);
- }
- }
- if ( gWhichOffscreenProc == kBlitPixieProc )
- {
- if ( spriteTestP->spriteWorldP->pixelDepth >= 8 )
- {
- (void)SWSetSpriteWorldOffscreenDrawProc(spriteTestP->spriteWorldP,
- BlitPixieRectDrawProc);
- }
- else if (SW_68K)
- {
- (void)SWSetSpriteWorldOffscreenDrawProc(spriteTestP->spriteWorldP,
- BlitPixieAllBitRectDrawProc);
- }
- }
- if ( gWhichScreenProc == kBlitPixieProc )
- {
- if ( spriteTestP->spriteWorldP->pixelDepth >= 8 )
- {
- (void)SWSetSpriteWorldScreenDrawProc(spriteTestP->spriteWorldP,
- BlitPixieRectDrawProc);
- }
- else if (SW_68K)
- {
- (void)SWSetSpriteWorldScreenDrawProc(spriteTestP->spriteWorldP,
- BlitPixieAllBitRectDrawProc);
- }
- }
-
- SWSetFrameInterlacingMode( spriteTestP->spriteWorldP->workFrameP, gInterlaced, gSkipOddLines );
- SWSetFrameInterlacingMode( spriteTestP->spriteWorldP->windowFrameP, gInterlaced, gSkipOddLines );
-
- if ( gInterlaced )
- {
- if ( gWhichOffscreenProc == kCopyBitsProc )
- {
- (void)SWSetSpriteWorldOffscreenDrawProc(spriteTestP->spriteWorldP,
- SWInterlacedCopyBitsDrawProc);
- }
- if ( gWhichScreenProc == kCopyBitsProc )
- {
- (void)SWSetSpriteWorldScreenDrawProc(spriteTestP->spriteWorldP,
- SWInterlacedCopyBitsDrawProc);
- }
-
- // paint window black, since we're only drawing every other scanline
- SWSetPortToWindow( spriteTestP->spriteWorldP );
- PaintRect( &spriteTestP->spriteWorldP->windowFrameP->frameRect );
-
- SWUpdateSpriteWorld( spriteTestP->spriteWorldP, true );
- }
-
- if ( gClearBackground )
- {
- if ( gWhichOffscreenProc == kBlitPixieProc &&
- spriteTestP->spriteWorldP->pixelDepth >= 8 )
- {
- (void)SWSetSpriteWorldOffscreenDrawProc(spriteTestP->spriteWorldP,
- BlitPixieClearDrawProc);
- }
- else if ( gWhichOffscreenProc == kCopyBitsProc ||
- spriteTestP->spriteWorldP->pixelDepth < 8 )
- {
- (void)SWSetSpriteWorldOffscreenDrawProc(spriteTestP->spriteWorldP,
- SWStdWorldClearDrawProc);
- }
-
- SWUpdateSpriteWorld( spriteTestP->spriteWorldP, true );
- }
-
- // Floor it!!!
- SWSetSpriteWorldMaxFPS( spriteTestP->spriteWorldP, 0 );
-
- ticks = TickCount();
-
-
- // the actual tests...
-
- // a straight blitter test -- no animation to screen
-
- if ( kBlitterTest )
- {
- srcRect = gMasterGlobeSprite->frameArray[0]->frameRect;
- destRect = srcRect;
- OffsetRect( &destRect, 3, 0 );
- Microseconds( &startMicroseconds );
-
- if ( spriteTestP->spriteWorldP->pixelDepth >= 8 )
- {
- for (frames = 0; frames < 10000; frames++ )
- {
- BlitPixieMaskDrawProc(
- gMasterGlobeSprite->frameArray[0],
- spriteTestP->spriteWorldP->workFrameP,
- &srcRect,
- &destRect);
- }
- }
- #if SW_68K
- else
- {
- for (frames = 0; frames < 10000; frames++ )
- {
- BlitPixieAllBitMaskDrawProc(
- gMasterGlobeSprite->frameArray[0],
- spriteTestP->spriteWorldP->workFrameP,
- &srcRect,
- &destRect);
- }
- }
- #endif
-
- Microseconds( &endMicroseconds );
- seconds = endMicroseconds.lo - startMicroseconds.lo;
- if ( endMicroseconds.hi != startMicroseconds.hi )
- seconds = 0; // do test over
- }
- else // a test of animation to screen
- {
- /*
- ProfilerSetStatus( true );
- */
- for (frames = 0; ((TickCount() - ticks) < kTestTime) && (!Button()); frames++)
- {
- if (gCollisionDetection)
- {
- SWCollideSpriteLayer(spriteTestP->spriteWorldP,
- spriteTestP->globeSpriteLayerP, spriteTestP->globeSpriteLayerP);
- }
-
- SWProcessSpriteWorld( spriteTestP->spriteWorldP );
- SWAnimateSpriteWorld( spriteTestP->spriteWorldP );
- }
-
- seconds = ((TickCount() - ticks) / kTicksPerSecond);
- }
- /*
- ProfilerSetStatus( false );
- ProfilerDump( "\pSWPPC Profile" );
- ProfilerTerm();
- */
-
- SWSetFrameInterlacingMode( spriteTestP->spriteWorldP->workFrameP, false, false );
- SWSetFrameInterlacingMode( spriteTestP->spriteWorldP->windowFrameP, false, false );
-
- // restore things to default state
- RestoreFromTest( testWindowP, spriteTestP );
-
- if ( gInterlaced )
- {
- SWUpdateSpriteWorld( spriteTestP->spriteWorldP, true );
-
- // update windows in front (e.g. AppSwitcher), since interlacing erased them
- PaintBehind( (WindowPtr) spriteTestP->spriteWorldP->windowFrameP->framePort,
- ((WindowPeek) (spriteTestP->spriteWorldP->windowFrameP))->strucRgn );
-
- // note: should redraw ControlStrip here too, but no idea how to do that ?
- }
-
- if ( gClearBackground )
- {
- SWUpdateSpriteWorld( spriteTestP->spriteWorldP, true );
- }
-
- gLastTestFrames = frames;
- gLastTestSeconds = seconds;
- }
-
-
-
- /******************** RestoreFromTest ********************/
- void RestoreFromTest(
- WindowPtr testWindowP,
- SpriteTestPtr spriteTestP)
- {
- SpritePtr curSpriteP;
-
-
- // restore things to default state
-
- curSpriteP = NULL;
-
- while ((curSpriteP = SWGetNextSprite(spriteTestP->globeSpriteLayerP, curSpriteP)) != NULL)
- {
- (void)SWSetSpriteDrawProc(curSpriteP, SWStdSpriteDrawProc);
-
- if (curSpriteP != spriteTestP->titleSpriteP && curSpriteP != spriteTestP->twoSpriteP)
- {
- SWSetSpriteFrameTime(curSpriteP, kGlobeSpriteFrameTime);
- SWSetSpriteCollideProc(curSpriteP, RegionBounceCollideProc);
- }
- }
-
- (void)SWSetSpriteWorldOffscreenDrawProc(spriteTestP->spriteWorldP, SWStdWorldDrawProc);
- (void)SWSetSpriteWorldScreenDrawProc(spriteTestP->spriteWorldP, SWStdWorldDrawProc);
-
- SWSetSpriteWorldMaxFPS( spriteTestP->spriteWorldP, 30 );
-
- ShowCursor();
- SWShowMenuBar(testWindowP);
- }
-
- #pragma mark -
-
- /******************** DisplayPerformance ********************/
- void DisplayPerformance(
- long frames,
- float seconds)
- {
- Str255 framesString, secondsString, fpsString;
- float fps;
-
- NumToString(frames, framesString);
- NumToString(seconds, secondsString);
-
- fps = (seconds > 0.0) ? (float) frames / seconds : frames;
-
- NumToString(fps, fpsString);
-
- ParamText(framesString, secondsString, fpsString, "\p");
- NoteAlert(kPerformanceAlertID, NULL);
- }
-
-
- /******************** GlobeSpriteMoveProc ********************/
- SW_FUNC void GlobeSpriteMoveProc(SpritePtr globeSpriteP)
- {
- SWOffsetSprite(globeSpriteP, globeSpriteP->horizMoveDelta, globeSpriteP->vertMoveDelta);
- (void)SWBounceSprite(globeSpriteP);
- }
-
- /******************** RectBounceCollideProc ********************/
- SW_FUNC void RectBounceCollideProc(
- SpritePtr srcSpriteP,
- SpritePtr dstSpriteP,
- Rect* sectRect)
- {
-
- // If both sprites use the same collision routine (this one),ignore the second collision.
- if ((!srcSpriteP->isVisible || !dstSpriteP->isVisible) ||
- ((srcSpriteP->spriteCollideProc == dstSpriteP->spriteCollideProc) &&
- (srcSpriteP > dstSpriteP)))
- {
- return;
- }
-
- if ( true )
- {
- // if a stationary sprite (the title), just bounce off it
- if ((dstSpriteP->horizMoveDelta == 0) && (dstSpriteP->vertMoveDelta == 0))
- {
- BounceGlobeOffTitle( dstSpriteP, srcSpriteP, sectRect );
- }
- else
- // globe to globe collision; swap movement delta's
- {
- BounceGlobeOffGlobe( srcSpriteP, dstSpriteP );
- }
- }
- }
- /******************** RLEBounceCollideProc ********************/
- SW_FUNC void RLEBounceCollideProc(
- SpritePtr srcSpriteP,
- SpritePtr dstSpriteP,
- Rect* sectRect)
- {
-
- // If both sprites use the same collision routine (this one),ignore the second collision.
- if ((!srcSpriteP->isVisible || !dstSpriteP->isVisible) ||
- ((srcSpriteP->spriteCollideProc == dstSpriteP->spriteCollideProc) &&
- (srcSpriteP > dstSpriteP)))
- {
- return;
- }
-
-
- if ( SWRLECollision(srcSpriteP, dstSpriteP) )
- {
- // if a stationary sprite (the title), just bounce off it
- if ((dstSpriteP->horizMoveDelta == 0) && (dstSpriteP->vertMoveDelta == 0))
- {
- BounceGlobeOffTitle( dstSpriteP, srcSpriteP, sectRect );
- }
- else
- // globe to globe collision; swap movement delta's
- {
- BounceGlobeOffGlobe( srcSpriteP, dstSpriteP );
- }
- }
- }
-
- /******************** PixelBounceCollideProc ********************/
- SW_FUNC void PixelBounceCollideProc(
- SpritePtr srcSpriteP,
- SpritePtr dstSpriteP,
- Rect* sectRect)
- {
-
- // If both sprites use the same collision routine (this one),ignore the second collision.
- if ((!srcSpriteP->isVisible || !dstSpriteP->isVisible) ||
- ((srcSpriteP->spriteCollideProc == dstSpriteP->spriteCollideProc) &&
- (srcSpriteP > dstSpriteP)))
- {
- return;
- }
-
- if ( SWPixelCollision(srcSpriteP, dstSpriteP) )
- {
- // if a stationary sprite (the title), just bounce off it
- if ((dstSpriteP->horizMoveDelta == 0) && (dstSpriteP->vertMoveDelta == 0))
- {
- BounceGlobeOffTitle( dstSpriteP, srcSpriteP, sectRect );
- }
- else
- // globe to globe collision; swap movement delta's
- {
- BounceGlobeOffGlobe( srcSpriteP, dstSpriteP );
- }
- }
- }
-
-
- /******************** RegionBounceCollideProc ********************/
- SW_FUNC void RegionBounceCollideProc(
- SpritePtr srcSpriteP,
- SpritePtr dstSpriteP,
- Rect* sectRect)
- {
-
- // If both sprites use the same collision routine (this one),ignore the second collision.
- if ((!srcSpriteP->isVisible || !dstSpriteP->isVisible) ||
- ((srcSpriteP->spriteCollideProc == dstSpriteP->spriteCollideProc) &&
- (srcSpriteP > dstSpriteP)))
- {
- return;
- }
-
-
- if ( SWRegionCollision(srcSpriteP, dstSpriteP) )
- {
- // if a stationary sprite (the title), just bounce off it
- if ((dstSpriteP->horizMoveDelta == 0) && (dstSpriteP->vertMoveDelta == 0))
- {
- BounceGlobeOffTitle( dstSpriteP, srcSpriteP, sectRect );
- }
- else
- // globe to globe collision; swap movement delta's
- {
- BounceGlobeOffGlobe( srcSpriteP, dstSpriteP );
- }
- }
- }
-
-
- /******************** RadiusBounceCollideProc ********************/
- SW_FUNC void RadiusBounceCollideProc(
- SpritePtr srcSpriteP,
- SpritePtr dstSpriteP,
- Rect* sectRect)
- {
- #pragma unused(sectRect)
-
- // If both sprites use the same collision routine (this one),ignore the second collision.
- if ((!srcSpriteP->isVisible || !dstSpriteP->isVisible) ||
- ((srcSpriteP->spriteCollideProc == dstSpriteP->spriteCollideProc) &&
- (srcSpriteP > dstSpriteP)))
- return;
-
- // ignore collision with title sprite
- if ( (srcSpriteP->destFrameRect.right-SWGetSpriteHorizLoc(srcSpriteP) !=
- srcSpriteP->destFrameRect.bottom-SWGetSpriteVertLoc(srcSpriteP)) ||
- (dstSpriteP->destFrameRect.right-dstSpriteP->destFrameRect.left !=
- dstSpriteP->destFrameRect.bottom-dstSpriteP->destFrameRect.top))
- return;
-
- if ( SWRadiusCollision( srcSpriteP, dstSpriteP ) )
- {
- BounceGlobeOffGlobe( srcSpriteP, dstSpriteP );
- }
- }
-
-
- /******************** BounceGlobeOffGlobe ********************/
- void BounceGlobeOffGlobe(
- SpritePtr srcSpriteP,
- SpritePtr dstSpriteP )
- {
- short tempDelta;
- short nextHorizDistA, nextVertDistA,
- nextHorizDistB, nextVertDistB;
-
-
- // reverse spins.
- srcSpriteP->frameAdvance = -srcSpriteP->frameAdvance;
- dstSpriteP->frameAdvance = -dstSpriteP->frameAdvance;
-
- // Calculate what the distance between sprites will be if we don't switch deltas
- nextHorizDistA = SWGetSpriteHorizLoc(srcSpriteP) + srcSpriteP->horizMoveDelta -
- (dstSpriteP->destFrameRect.left + dstSpriteP->horizMoveDelta);
- if (nextHorizDistA < 0)
- nextHorizDistA = -nextHorizDistA;
-
- nextVertDistA = SWGetSpriteVertLoc(srcSpriteP) + srcSpriteP->vertMoveDelta -
- (dstSpriteP->destFrameRect.top + dstSpriteP->vertMoveDelta);
- if (nextVertDistA < 0)
- nextVertDistA = -nextVertDistA;
-
- // Calculate what the distance between sprites will be if we do switch deltas
- nextHorizDistB = SWGetSpriteHorizLoc(srcSpriteP) + dstSpriteP->horizMoveDelta -
- (dstSpriteP->destFrameRect.left + srcSpriteP->horizMoveDelta);
- if (nextHorizDistB < 0)
- nextHorizDistB = -nextHorizDistB;
-
- nextVertDistB = SWGetSpriteVertLoc(srcSpriteP) + dstSpriteP->vertMoveDelta -
- (dstSpriteP->destFrameRect.top + srcSpriteP->vertMoveDelta);
- if (nextVertDistB < 0)
- nextVertDistB = -nextVertDistB;
-
-
- // Will swapping the horizontal deltas move the sprites farther apart?
- if (nextHorizDistB > nextHorizDistA)
- {
- // swap horizontal deltas
- tempDelta = srcSpriteP->horizMoveDelta;
- srcSpriteP->horizMoveDelta = dstSpriteP->horizMoveDelta;
- dstSpriteP->horizMoveDelta = tempDelta;
- }
-
- // Will swapping the vertical deltas move the sprites farther apart?
- if (nextVertDistB > nextVertDistA)
- {
- // swap vertical deltas
- tempDelta = srcSpriteP->vertMoveDelta;
- srcSpriteP->vertMoveDelta = dstSpriteP->vertMoveDelta;
- dstSpriteP->vertMoveDelta = tempDelta;
- }
- }
-
-
- /******************** BounceGlobeOffTitle ********************/
- void BounceGlobeOffTitle(
- SpritePtr titleSpriteP,
- SpritePtr globeSpriteP,
- Rect* sectRect )
- {
- short absHorizDelta,
- absVertDelta;
-
- absHorizDelta = globeSpriteP->horizMoveDelta;
- if ( absHorizDelta < 0 )
- absHorizDelta = -absHorizDelta;
- absVertDelta = globeSpriteP->vertMoveDelta;
- if ( absVertDelta < 0 )
- absVertDelta = -absVertDelta;
-
- // draw a picture and this test algorithm will become clear
- if ((sectRect->right - sectRect->left) < (sectRect->bottom - sectRect->top) )
- {
- // Hit left or right side
- if ( sectRect->left <= titleSpriteP->destFrameRect.left )
- // hit on left side
- globeSpriteP->horizMoveDelta = -absHorizDelta;
- else
- // hit on right side
- globeSpriteP->horizMoveDelta = absHorizDelta;
- }
- else
- {
- // Hit top or bottom
- if ( sectRect->top <= titleSpriteP->destFrameRect.top )
- // hit on top
- globeSpriteP->vertMoveDelta = -absVertDelta;
- else
- // hit on bottom
- globeSpriteP->vertMoveDelta = absVertDelta;
- }
- }
-